<?php

namespace App\Http\Controllers\user;

use App\Http\Controllers\Controller;
use App\Http\Requests\ChangeUsernameRequest;
use App\Http\Requests\ModifyPasswordRequest;
use App\Http\Requests\VerifyIndexLogin;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{

    Public function login(VerifyIndexLogin $request)
    {
        $account = $request['account'];
        $password = $request['password'];
        $state = $request['status'];

        // 验证用户名登录方式
        $user = Auth::guard('web')->attempt(
            [
                'account' => $account,
                'password' => $password,
            ]);

        if ($user) {

            if ((int)$state == 1) {
                $status = DB::table('users')->where('account', $account)->select('status')->first();

                if ($status->status > 0) {
                    $data = [
                        'url' => "UR2 "         //  普通用户(前台页面)
                    ];
                } else {
                    $data = [
                        'url' => "UR2 "         //  超管用户(前台页面)
                    ];
                }
                return response()->success(200, '成功', $data);
            } else if ((int)$state == 0) {
                $status = DB::table('users')->where('account', $account)->select('status')->first();

                if ($status->status == 0 or $status->status == -1) {
                    $data = [
                        'url' => "UR1"        // 超管或者管理员
                    ];
                    return response()->success(200, '成功', $data);
                } else {
                    $data = null;
                    return response()->fail(100, '用户权限不够', null);
                }
            }
        } else {
            $data = null;
            return response()->fail(100, '账号或密码错误', $data);
        }

    }

    


    Public function loginout()
    {

        Auth::guard('web')->logout();
        return response()->success(200, '成功', null);

        /* return redirect('/login');*/
    }


    Public function modifypassword(ModifyPasswordRequest $request)
    {
        $id = Auth::id();
        $old_password = $request['old_password'];
        $new_password = $request['new_password'];
        $new_password_confirmation = $request['new_password_confirmation'];

        $res_password = DB::table('users')->where('id', $id)->select('password')->first();   //$res_password 数据库的密码（一个对象）
        /*dd($res_password->password);    //  数据库密码*/

        /* $decyrpt = bcrypt($old_password);       //新密码加密,bcrypt能对空加密*/


        if (Hash::check($old_password, $res_password->password)) {

            $password = bcrypt($new_password);
            $data = DB::table('users')->where('id', $id)->select('password')->update([
                'password' => $password,
            ]);

            return response()->success(200, '成功', null);
        } else {
            return response()->fail(100, '旧密码不正确', null);
        }

    }


    public function getUserInfo(Request $request)
    {
        $id = Auth::id();
        // $id = 3;
        $data = DB::table('users')->where('id', $id)->first();
        $posts = DB::table('posts')->where('user_id', $id)->select('id')->first();
        $result = [
            'code' => 200,
            'msg' => '成功',
            'data' => array([
                'account' => $data->account,
                'name' => $data->user_name,
                'number' => $posts->id,
                'rating' => $data->rating,
                'status' => $data->status
            ])
        ];
        return response()->json($result);

//            $thirds=DB::table('users as U' )->join('posts as P','U.id', 'P.user_id')->where('U.id',$id)->select('account','user_name','rating')->get();

    }


    public function updateUsername(ChangeUsernameRequest $request)
    {
        $id = Auth::id();
        $id = 3;

        $new_user_name = $request['user_name'];

        $data = DB::table('users')->where('id', $id)->update(['user_name' => $new_user_name]);

        return response()->success(200, '成功', null);
    }


    //测试
    public function home()
    {

//        $password='123456';
        $password = bcrypt('123456');

//        $passwordHash = password_hash($password, PASSWORD_BCRYPT);
        //dd($passwordHash);

        $result = User::create([
            'account' => '17310520420',
            'password' => $password,
            'user_name' => '123456',
            'status' => '5',

        ]);
        dd($result);

    }
}
